package com.jsnkj.basic.fragment

import android.graphics.Bitmap
import android.os.Bundle
import android.view.View
import com.jsnkj.basic.R
import com.jsnkj.basic.activity.GoInviteActivity
import com.jsnkj.basic.activity.MyApprenticeActivity
import com.jsnkj.basic.activity.MyEarningsActivity
import com.jsnkj.basic.activity.MyInformationActivity
import com.jsnkj.basic.activity.MyPurseActivity
import com.jsnkj.basic.config.SpConfig
import com.jsnkj.basic.databinding.FragmentInviteFriendsBinding
import com.jsnkj.basic.dialog.JoinPlayerDialog
import com.jsnkj.basic.mvvm.BaseMVVMFragment
import com.jsnkj.basic.util.MediaUtil
import com.jsnkj.basic.util.SharePreferencesUtil
import com.bumptech.glide.Glide

//邀请fragment
class InviteFriendsFragment :
    BaseMVVMFragment<FragmentInviteFriendsBinding, InviteFriendsViewModel>(), View.OnClickListener {

    companion object {
        fun newInstance() = InviteFriendsFragment()
    }

    override val layoutId: Int
        get() = R.layout.fragment_invite_friends


    var joinPlayerDialog: JoinPlayerDialog? = null


    override fun initView(savedInstanceState: Bundle?) {
        mVDB.clickListener = this
        MediaUtil.heartBeat(mVDB.imgImmediatePromotion)
    }

    override fun bindEvent() {
        super.bindEvent()
        mVM.postHttpResult.observe(this) {
            dismissDialog()
            if (it.share_link.isNullOrEmpty()) {
                com.jsnkj.basic.util.ToastUtils.show("邀请失败")
                return@observe
            }

            GoInviteActivity.goToInvite(requireActivity(), it.share_link + "&cid=shagnwan")
        }


        mVM.loginEntityResult.observe(this) { user ->
            com.jsnkj.basic.util.LogUtils.e("邀请fragment user  数据： $user")

            if (user.avatar.isNullOrEmpty()){
                Glide.with(requireActivity()).load(R.drawable.icon_user).into(mVDB.imgIcon)
            }else{
                Glide.with(requireActivity()).load(user.avatar).into(mVDB.imgIcon)
            }

            mVDB.txtName.text = user.showName()
            mVDB.txtMineId.text = "会员ID：${user.uuid}"
            if (user.isVipGroup()) {
                mVDB.imgCommander.visibility = View.VISIBLE
            } else {
                mVDB.imgCommander.visibility = View.GONE
            }
        }

        mVM.postPopularizeResult.observe(this) { entity ->
            mVDB.txtMyFans.text = "${entity.getFans()}"
            mVDB.txtHeadcount.text = "${entity.getInvites()?.total}"
            mVDB.txtNewToday.text = "${entity.getInvites()?.today}"
            mVDB.txtAddedYesterday.text = "${entity.getInvites()?.yesterday}"
            mVDB.txtTodayEarnings.text = "${entity.getCommissions()?.today}"
            mVDB.txtMyWallet.text = "${entity.getCommissions()?.total}"
            mVDB.txtMyEarnings.text = "${entity.getCommissions()?.yesterday}"

        }
    }


    override fun onClick(view: View) {
        when (view.id) {
            R.id.img_immediate_promotion -> {
                showDialog()
                mVM.postUserInviteCode()
            }

            R.id.img_join_player -> {

                var content = SharePreferencesUtil.getInstance().getSettingEntity().player_group_url
                if (content.isNullOrEmpty()) {
                    com.jsnkj.basic.util.ToastUtils.show("数据获取失败，请稍后再试")
                    return
                }
                if (joinPlayerDialog?.isShowing == true) {
                    joinPlayerDialog?.dismiss()
                }

                joinPlayerDialog = JoinPlayerDialog(requireActivity(), content,
                    object : JoinPlayerDialog.onItemClickListener {
                        override fun saveItemClick(bitmap: Bitmap?) {
                            if (null == bitmap) {
                                com.jsnkj.basic.util.ToastUtils.show("保存失败")
                                return
                            }
                            MediaUtil.savePuzzleBitmap(
                                requireActivity(), bitmap, SpConfig.IMG_SAVE_NAME
                            ) { state, _ ->
                                if (state) {
                                    bitmap.recycle()
                                    com.jsnkj.basic.util.ToastUtils.show("已成功保存到相册")
                                } else {
                                    com.jsnkj.basic.util.ToastUtils.show("保存失败，请重试")
                                }
                            }
                        }
                    })

                joinPlayerDialog?.show()
            }

            R.id.con_layout_my_wallet -> {
//                ToastUtils.show("我的钱包")
                startActivity(requireActivity(), MyPurseActivity::class.java)
            }

            R.id.con_layout_my_earnings -> {
//                ToastUtils.show("我的收益")
                startActivity(requireActivity(), MyEarningsActivity::class.java)
            }

            R.id.con_layout_my_apprentice -> {
//                ToastUtils.show("我的徒弟")
                startActivity(requireActivity(), MyApprenticeActivity::class.java)
            }

            R.id.con_layout_my_information -> {
                startActivity(requireActivity(), MyInformationActivity::class.java)
            }

        }
    }


    private var isInitFragment = false

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (!isInitFragment) {
            return
        }
        com.jsnkj.basic.util.LogUtils.e("setUserVisibleHint  是否可见： $isVisibleToUser")

        if (isVisibleToUser) {
            mVM.postPopularizeSummary()
            mVM.loginEntityResult.value = SharePreferencesUtil.getInstance().getUserBean()?.user
        }
    }

    override fun onResume() {
        super.onResume()
        isInitFragment = true
    }


}